﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using Microsoft.Kinect;
using Microsoft.Kinect.Toolkit;
using Microsoft.Kinect.Toolkit.Controls;

namespace FizzGame
{
    /// <summary>
    /// StartPage.xaml 的互動邏輯
    /// </summary>
    public partial class StartPage : Page
    {
        System.Windows.Threading.DispatcherTimer dispatcherTimer;
        Object rectobj = new Object();
        Object rectobj2 = new Object();
        Vector V = new Vector(8.0f,-5.0f);

        private KinectSensorChooser sensorChooser;

        public StartPage()
        {
            InitializeComponent();

            Loaded += OnLoaded;

            dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
            dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
            dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 10);
            dispatcherTimer.Start();
        }

        private void OnLoaded(object sender, RoutedEventArgs e) 
        { 
            this.sensorChooser = new KinectSensorChooser(); 
            this.sensorChooser.KinectChanged += SensorChooserOnKinectChanged; 
            this.sensorChooserUi.KinectSensorChooser = this.sensorChooser;
            this.sensorChooser.Start(); 

            var regionSensorBinding = new Binding("Kinect") { Source = this.sensorChooser };
            BindingOperations.SetBinding(this.kinectRegion, KinectRegion.KinectSensorProperty, regionSensorBinding);
             button.Click += Button_Click_1;
      }

        private void SensorChooserOnKinectChanged(object sender, KinectChangedEventArgs args) 
        { 
            //MessageBox.Show(args.NewSensor == null ? "No Kinect" : args.NewSensor.Status.ToString());
            if (args.OldSensor != null)
            {
                try
                {
                    args.OldSensor.DepthStream.Range = DepthRange.Default;
                    args.OldSensor.SkeletonStream.EnableTrackingInNearRange = false;
                    args.OldSensor.DepthStream.Disable();
                    args.OldSensor.SkeletonStream.Disable();
                }
                catch (InvalidOperationException)
                {
                    // KinectSensor might enter an invalid state while enabling/disabling streams or stream features.
                    // E.g.: sensor might be abruptly unplugged.
                }
            }

            if (args.NewSensor != null)
            {
                try
                {
                    args.NewSensor.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30);
                    args.NewSensor.SkeletonStream.Enable();

                    try
                    {
                        args.NewSensor.DepthStream.Range = DepthRange.Near;
                        args.NewSensor.SkeletonStream.EnableTrackingInNearRange = true;
                    }
                    catch (InvalidOperationException)
                    {
                        // Non Kinect for Windows devices do not support Near mode, so reset back to default mode.
                        args.NewSensor.DepthStream.Range = DepthRange.Default;
                        args.NewSensor.SkeletonStream.EnableTrackingInNearRange = false;
                    }
                }
                catch (InvalidOperationException)
                {
                    // KinectSensor might enter an invalid state while enabling/disabling streams or stream features.
                    // E.g.: sensor might be abruptly unplugged.
                }
            }
        }
        int iframe = 1;
        void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            Title.Source = new BitmapImage(new Uri(@"C:\Users\意喬\SkyDrive\Documents\程式專案\FizzGame\FizzGame\Image\Title\Fizzy ("+iframe.ToString()+").png"));
            if (iframe < 124)
                iframe++;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            MenuPage menupage = new MenuPage();
            this.NavigationService.Navigate(menupage);
            Clean();
        }
        private void Clean()
        {
            this.sensorChooser.KinectChanged -= SensorChooserOnKinectChanged; 
            this.sensorChooser.Stop();
          //  this.sensorChooser = null;
        }
    }
}
